﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Trippy.Levels;

namespace Trippy.UI_Overlay
{
    class TitleScreen : TrippyState
    {
        List<MenuWindow> menuList = new List<MenuWindow>();
        public new TrippyGame Game { get; protected set; }
        public MenuWindow menuMain { get; protected set; }
        MenuWindow activeMenu;
        MenuWindow closeGame;
        MenuWindow menuNewGame;
        MenuWindow menuConfig;
        MenuWindow goBackfromConfig;
        MenuWindow leftConfig;
        MenuWindow downConfig;
        MenuWindow rightConfig;
        MenuWindow jumpConfig;
        MenuWindow actionConfig;
        MenuWindow inGameMenuConfig;
        KeyboardState CurKeyState;
        KeyboardState PrevKeyState;
        private SpriteBatch spriteBatch;

        public TitleScreen(TrippyGame game)
            : base(game)
        {
            Game = game;
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
        }
        protected override void LoadContent()
        {
            SpriteFont menuFont = Game.Content.Load<SpriteFont>("Fonts/UI");
            Texture2D background = Game.Content.Load<Texture2D>("Background/abstract_bkg");
            MediaPlayer.Play(Game.menuSong);
            MediaPlayer.IsRepeating = true;
            //Creating menuItems and adding to the menuList

            menuMain = new MenuWindow(menuFont, "", background);
            menuNewGame = new MenuWindow(null, null, null);
            leftConfig = new MenuWindow(null, null, null);
            jumpConfig = new MenuWindow(null, null, null);
            downConfig = new MenuWindow(null, null, null);
            rightConfig = new MenuWindow(null, null, null);
            actionConfig = new MenuWindow(null, null, null);
            inGameMenuConfig = new MenuWindow(null, null, null);
            menuConfig = new MenuWindow(menuFont, "Key Configuration", background);

            goBackfromConfig = new MenuWindow(null, null, null);
            closeGame = new MenuWindow(null, null, null);

            activeMenu = menuMain;
            menuList.Add(menuMain);
            menuList.Add(menuNewGame);
            menuList.Add(menuConfig);

            menuMain.AddMenuItem("New Game", menuNewGame);
            menuMain.AddMenuItem("Configuration", menuConfig);
            menuConfig.AddMenuItem("Left               Key: " + Game.left.ToString(), leftConfig);
            menuConfig.AddMenuItem("Right              Key: " + Game.right.ToString(), rightConfig);
            menuConfig.AddMenuItem("Up/Jump            Key: " + Game.jump.ToString(), jumpConfig);
            menuConfig.AddMenuItem("Action             Key: " + Game.action.ToString(), actionConfig);
            menuConfig.AddMenuItem("Menu               Key: " + Game.menu.ToString(), inGameMenuConfig);
            menuConfig.AddMenuItem("Back", menuMain);
            menuMain.AddMenuItem("Quit", closeGame);

            menuMain.WakeUp();
        }

        public override void Update(GameTime gameTime)
        {
            CurKeyState = Keyboard.GetState();

            foreach (MenuWindow currentMenu in menuList)
                currentMenu.Update(gameTime.ElapsedGameTime.TotalMilliseconds);
            MenuInput(CurKeyState);
            
            base.Update(gameTime);
            PrevKeyState = CurKeyState;
        }

        public override void Draw(GameTime gametime)
        {
            spriteBatch.Begin();
            foreach (MenuWindow currentMenu in menuList)
                currentMenu.Draw(spriteBatch);
            spriteBatch.End();
        }

        private void MenuInput(KeyboardState currentKeybState)
        {
            MenuWindow newActive = activeMenu.ProcessInput(PrevKeyState, currentKeybState);

           // if (newActive.windowState != MenuWindow.WindowState.Starting)
           // {
                if (newActive == menuNewGame)
                {
                    var firstLevel = new Levels.FirstLevel(Game, null); // if no hero is passed as a parameter, one is automatically created
                    InGamePause.savedLevel = firstLevel;

                    //initializing save list
                    foreach (GameComponent x in firstLevel.Objects)
                        InGamePause.saveList.Add(x);

                    //var bossLevel = new BossFight1(Game, null);

                    Game.Components.Clear();

                    Game.Components.Add(firstLevel);

                    Game.currentState = firstLevel;

                }
                if (newActive == leftConfig && currentKeybState.GetPressedKeys().Length > 0)
                {
                    if (currentKeybState.GetPressedKeys()[0] == Keys.Enter)
                    {
                        menuConfig.active = true;
                        activeMenu.Active();
                        newActive = leftConfig;
                    }
                    if (currentKeybState.GetPressedKeys()[0] != Keys.Enter)
                    {
                        Keys pressedKey = currentKeybState.GetPressedKeys()[0];
                        Game.left = pressedKey;
                        menuConfig.itemList[0].itemText = "Left               Key: " + Game.left.ToString();
                        menuConfig.active = false;
                        activeMenu.Active();
                        activeMenu = newActive = menuConfig;
                    }
                }
                if (newActive == rightConfig && currentKeybState.GetPressedKeys().Length > 0)
                {
                    if (currentKeybState.GetPressedKeys()[0] == Keys.Enter)
                    {
                        menuConfig.active = true;
                        activeMenu.Active();
                        newActive = rightConfig;
                    }
                    if (currentKeybState.GetPressedKeys()[0] != Keys.Enter)
                    {
                        Keys pressedKey = currentKeybState.GetPressedKeys()[0];
                        Game.right = pressedKey;
                        menuConfig.itemList[1].itemText = "Right              Key: " + Game.right.ToString();
                        menuConfig.active = false;
                        activeMenu.Active();
                        activeMenu = newActive = menuConfig;
                    }
                }
                if (newActive == jumpConfig && currentKeybState.GetPressedKeys().Length > 0)
                {
                    if (currentKeybState.GetPressedKeys()[0] == Keys.Enter)
                    {
                        menuConfig.active = true;
                        activeMenu.Active();
                        newActive = jumpConfig;
                    }
                    if (currentKeybState.GetPressedKeys()[0] != Keys.Enter)
                    {
                        Keys pressedKey = currentKeybState.GetPressedKeys()[0];
                        Game.jump = pressedKey;
                        menuConfig.itemList[2].itemText = "Up/Jump            Key: " + Game.jump.ToString();
                        menuConfig.active = false;
                        activeMenu.Active();
                        activeMenu = newActive = menuConfig;
                    }
                }
                if (newActive == actionConfig && currentKeybState.GetPressedKeys().Length > 0)
                {
                    if (currentKeybState.GetPressedKeys()[0] == Keys.Enter)
                    {
                        menuConfig.active = true;
                        activeMenu.Active();
                        newActive = actionConfig;
                    }
                    if (currentKeybState.GetPressedKeys()[0] != Keys.Enter)
                    {
                        Keys pressedKey = currentKeybState.GetPressedKeys()[0];
                        Game.action = pressedKey;
                        menuConfig.itemList[3].itemText = "Action             Key: " + Game.action.ToString();
                        menuConfig.active = false;
                        activeMenu.Active();
                        activeMenu = newActive = menuConfig;
                    }
                }
                if (newActive == inGameMenuConfig && currentKeybState.GetPressedKeys().Length > 0)
                {
                    if (currentKeybState.GetPressedKeys()[0] == Keys.Enter)
                    {
                        menuConfig.active = true;
                        activeMenu.Active();
                        newActive = inGameMenuConfig;
                    }
                    if (currentKeybState.GetPressedKeys()[0] != Keys.Enter)
                    {
                        Keys pressedKey = currentKeybState.GetPressedKeys()[0];
                        Game.menu = pressedKey;
                        menuConfig.itemList[4].itemText = "Menu               Key: " + Game.menu.ToString();
                        menuConfig.active = false;
                        activeMenu.Active();
                        activeMenu = newActive = menuConfig;
                    }
                }
                if (newActive == closeGame)
                    Game.Exit();
                if (newActive == null)
                    Game.Exit();
                else if (newActive != activeMenu)
                    newActive.WakeUp();

                activeMenu = newActive;
            }
        //}
    }
}
